package xml;

import java.net.*;
import java.io.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;

public class testXML {
	
	public static void main(String[] args) {
		createXMLDocument();
		readXMLDocument();
		
	}
	
	public static void createXMLDocument() {
		
		try {
			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			
			Document page = builder.newDocument();
			Element request = page.createElement("Test");
			page.appendChild(request);
			
			Element generation = page.createElement("generation");
			request.appendChild(generation);
			
			// generation number
			Element number = page.createElement("number");
			Text text = page.createTextNode("1");
			number.appendChild(text);
			generation.appendChild(number);
			
			//weightSet
			Element weightSet = page.createElement("weightSet");
			generation.appendChild(weightSet);
			
			//weight 1
			Element weight = page.createElement("w1");
			Element value = page.createElement("value");
			Element dval = page.createElement("double");
			text = page.createTextNode("2.34567");
			dval.appendChild(text);
			value.appendChild(dval);
			weight.appendChild(value);
			weightSet.appendChild(weight);
			
			//weight 2
			weight = page.createElement("w2");
			value = page.createElement("value");
			dval = page.createElement("double");
			text = page.createTextNode("3.1415926");
			dval.appendChild(text);
			value.appendChild(dval);
			weight.appendChild(value);
			weightSet.appendChild(weight);
			
			OutputStream fout = new FileOutputStream("request.xml");
			OutputStream bout = new BufferedOutputStream(fout);
			OutputStreamWriter out = new OutputStreamWriter(bout,"8859_1");
			
			TransformerFactory xformFactory = TransformerFactory.newInstance();
			Transformer idTransform = xformFactory.newTransformer();
			Source input = new DOMSource(request);
			Result output = new StreamResult(out);
			idTransform.transform(input,output);
			out.flush();
			out.close();
			
		} catch (Exception e) {
			System.err.println(e);
			e.printStackTrace();
		}
	}
	
	public static void readXMLDocument() {
		try {
			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = builderFactory.newDocumentBuilder();
			
			InputStream fin = new FileInputStream("request.xml");
			InputStream bin = new BufferedInputStream(fin);
			//InputStreamReader in = new InputStreamReader(bin);
			Document response = builder.parse(bin);
			
			bin.close();
			
			NodeList ws = response.getElementsByTagName("weightSet");
			for(int i=0; i<ws.getLength(); i++) { 
				for(int j=0; j<ws.item(i).getChildNodes().getLength(); j++) {
					Element w = (Element)ws.item(i).getChildNodes().item(j);
					String v = w.getElementsByTagName("double").item(0).getFirstChild().getNodeValue();
					Double d = Double.valueOf(v);
					System.out.println(""+w.getNodeName()+": "+d);
				}
				
				
			}
			/*
			for (int i=0; i<search.getLength(); i++) {
				Node datum = search.item(i);
				Text result = (Text)datum.getFirstChild();
				System.out.println(result.getNodeValue());
			}
			*/
			
			Element doc = (Element)response.getFirstChild();
			
			Element generation = response.createElement("generation");
			doc.appendChild(generation);
			
			// generation number
			Element number = response.createElement("number");
			Text text = response.createTextNode("2");
			number.appendChild(text);
			generation.appendChild(number);
			
			//weightSet
			Element weightSet = response.createElement("weightSet");
			generation.appendChild(weightSet);
			
			//weight 1
			Element weight = response.createElement("w1");
			Element value = response.createElement("value");
			Element dval = response.createElement("double");
			text = response.createTextNode("2.34567");
			dval.appendChild(text);
			value.appendChild(dval);
			weight.appendChild(value);
			weightSet.appendChild(weight);
			
			//weight 2
			weight = response.createElement("w2");
			value = response.createElement("value");
			dval = response.createElement("double");
			text = response.createTextNode("3.1415926");
			dval.appendChild(text);
			value.appendChild(dval);
			weight.appendChild(value);
			weightSet.appendChild(weight);
			
			OutputStream fout = new FileOutputStream("request.xml");
			OutputStream bout = new BufferedOutputStream(fout);
			OutputStreamWriter out = new OutputStreamWriter(bout,"8859_1");
			
			TransformerFactory xformFactory = TransformerFactory.newInstance();
			Transformer idTransform = xformFactory.newTransformer();
			Source input = new DOMSource(response);
			Result output = new StreamResult(out);
			idTransform.transform(input,output);
			out.flush();
			out.close();
			
			
		} catch (Exception e) {
			System.err.println(e);
			e.printStackTrace();
		}
		
	}
}
